<template>
  <div class="w-full">
    <div class="mb-2">
      <el-tag>{{ $t("client.actionNew") }}</el-tag>
    </div>

    <el-table :data="items" border stripe @expand-change="handleExpandChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div class="m-2">
            <div class="mb-2">
              <el-tag>{{ $t("client.device") }}</el-tag>
            </div>

            <el-table
              v-if="loading.devices"
              :data="devices[scope.row['_id']]"
              border
            >
              <el-table-column prop="date" label="日期" width="180">
              </el-table-column>
              <el-table-column prop="name" label="姓名" width="180">
              </el-table-column>
              <el-table-column prop="address" label="地址"> </el-table-column>
            </el-table>
          </div>
          <div class="m-2">
            <div class="mb-2">
              <el-tag>{{ $t("client.app") }}</el-tag>
            </div>

            <el-table v-if="loading.apps" :data="apps[scope.row['_id']]" border>
              <el-table-column prop="name" :label="$t('app.name')">
              </el-table-column>
              <el-table-column :label="$t('app.state')" prop="state">
                <template slot-scope="scope">
                  <!-- 在线/离线 -->
                  <el-tag :type="stateColor[scope.row.state]">{{
                    $t("app." + scope.row.state)
                  }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column :label="$t('app.oper')" width="180px">
                <template slot-scope="scope">
                  <!-- 刷新状态 -->
                  <el-button
                    type="primary"
                    icon="el-icon-refresh-right"
                    size="mini"
                    @click="handleRefreshApp(scope.row['_id'])"
                  ></el-button>
                  <!-- 删除 -->
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    @click="handleDeletApp(scope.row['_id'])"
                    v-if="isAdmin"
                  ></el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('client.name')" prop="name"></el-table-column>
      <el-table-column :label="$t('client.twoFactor')" prop="state">
        <template slot-scope="scope">
          <!-- Two factor authentication -->
          <el-switch
            v-model="scope.row.two_factor"
            @change="handleUpdateClient(scope.row)"
            :disabled="!isAdmin"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column :label="$t('client.oper')" width="180px">
        <template slot-scope="scope">
          <!-- 刷新状态 -->
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            size="mini"
            @click="handleRefreshClient(scope.row['_id'])"
          ></el-button>
          <!-- 删除 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeletClient(scope.row['_id'])"
            v-if="isAdmin"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-pagination
      layout="total, prev, pager, next"
      background
      :page-size="pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      style="text-align: center; margin-top: 20px"
    >
    </el-pagination>
  </div>
</template>


<script>
import { mapGetters } from "vuex";
import { getClients, getClientDevices, getClientApps } from "@/api/client";

export default {
  computed: {
    ...mapGetters(["isAdmin"]),
  },
  props: ["index", "keyword"],
  data() {
    return {
      page: 1,
      pageSize: 10,
      items: [],
      total: 0,

      devices: {},
      apps: {},

      loading: {
        devices: false,
        apps: false,
      },

      stateColor: {
        active: "success",
        inactive: "info",
      },
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      getClients({
        type: this.index,
        keyword: this.keyword,
        page: this.page,
        pageSize: this.pageSize,
      }).then((res) => {
        this.total = res.data.total;
        this.items = res.data.list;
      });
    },
    async fetchDevices(id) {
      this.loading.devices = false;
      getClientDevices({ id }).then((res) => {
        this.devices[id] = res.data || [];
        this.loading.devices = true;
      });
    },
    async fetchApps(id) {
      this.loading.apps = false;
      getClientApps({ id }).then((res) => {
        this.apps[id] = res.data || [];
        this.loading.apps = true;
      });
    },
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.fetchData();
    },
    handleRefreshClient(id) {
      console.log(id);
    },
    handleDeletClient(id) {
      console.log(id);
    },
    handleUpdateClient(item) {
      console.log(item);
    },
    handleExpandChange(item, expandedRows) {
      let found = expandedRows.find((row) => {
        return row["_id"] === item["_id"];
      });
      if (found) {
        console.log("fetch");
        this.fetchDevices(item["_id"]);
        this.fetchApps(item["_id"]);
      }
    },
  },
};
</script>
